from django.shortcuts import render

# Create your views here.
from django.utils.decorators import method_decorator
from django.views import View
from django.http import HttpResponse


class RegisterView(View):
    """类视图, 类视图中的方法使用请求方法的小写命名"""
    def get(self, request):
        """处理GET请求,返回页面"""
        return HttpResponse('GET请求的业务处理')

    def post(self, request):
        """处理POST请求,逻辑处理"""
        return HttpResponse('POST请求的业务处理')


# 类视图使用装饰器
def my_decorator(func):
    def wrapper(request, *args, **kwargs):
        print('自定义装饰器被调用了')
        print('请求路径%s' % request.path)

        return func(request, *args, **kwargs)

    return wrapper


# name='dispatch' 在方法的上一级，为全部请求方法添加装饰器
# @method_decorator(my_decorator, name='dispatch')
# name='get' 为特定请求方法添加装饰器
# @method_decorator(my_decorator, name='get')
class DemoView(View):

    # @method_decorator(my_decorator)  # 为get方法添加装饰器
    def get(self, request):
        print('get方法')
        return HttpResponse('OK')

    @method_decorator(my_decorator)  # 为post方法添加装饰器
    def post(self, request):
        print('post方法')
        return HttpResponse('OK')


def index(request):
    """模板渲染"""
    # 获取模板
    context = {
        'city': '深圳',
        'adict': {
            'name': '周星驰',
            'book': '演员的自我修养'
        },
        'alist': [1, 2, 3, 4, 5, 6]
    }
    return render(request, 'index.html', context=context)
